package vn.co.thn.android.store.activity;

import java.util.ArrayList;

import vn.co.thn.android.store.NSUrlApi;
import vn.co.thn.android.store.R;
import vn.co.thn.android.store.adapter.NSCategoryListAdapter;
import vn.co.thn.android.store.data.NSCategoryListData;
import vn.co.thn.android.store.fragment.NSNewFeedFragment;
import vn.co.thn.android.store.fragment.NSStoreOnMapFragment;
import vn.co.thn.android.store.util.NSParam;
import vn.co.thn.android.store.util.NSRequestService;
import vn.co.thn.android.store.util.NSRequestService.NSRequestProcessUI;
import vn.co.thn.android.store.util.NSRequestService.NSRequestServiceCAllBack;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class NSMainActivity extends SlidingFragmentActivity implements OnClickListener,NSRequestProcessUI {
	private SlidingMenu mSlidingMenu;
	private ListView lst;
	private NSRequestService nsRequestService;
	private NSCategoryListData lst_data=new NSCategoryListData();
	private ProgressDialog progressDialog;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		progressDialog = new ProgressDialog(this);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(false);
		progressDialog.setMessage("loading...");
		replaceFragmentNoBackStackWithAnimation(new NSNewFeedFragment());
		setContentView(R.layout.ns_main_activity);
		setBehindContentView(R.layout.ns_left_menu);
		mSlidingMenu = getSlidingMenu();
		mSlidingMenu.setBehindOffsetRes(R.dimen._40dp);
		mSlidingMenu.setMode(SlidingMenu.LEFT);
		mSlidingMenu.setBehindScrollScale(0f);
		mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		findViewById(R.id.menu_bar).setOnClickListener(this);
		findViewById(R.id.search_bar).setOnClickListener(this);
		findViewById(R.id.post).setOnClickListener(this);
		mSlidingMenu.findViewById(R.id.home_menu).setOnClickListener(this);
		mSlidingMenu.findViewById(R.id.signup).setOnClickListener(this);
		lst = (ListView) findViewById(R.id.lst);
		
	}


	private void getData() {
		ArrayList<NSParam> params = new ArrayList<NSParam>();
		nsRequestService = new NSRequestService(NSUrlApi.Api_Category_List, NSCategoryListData.class, params,
				new NSRequestServiceCAllBack<NSCategoryListData>() {

					@Override
					public void onResult(NSCategoryListData data, boolean isUpdate) {
						// TODO Auto-generated method stub
						lst_data=data;
						lst.setAdapter(new NSCategoryListAdapter(getApplicationContext(), lst_data.getCategory_list()));
					}

					@Override
					public void onError(VolleyError error) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onException(Exception e) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onResult(ArrayList<NSCategoryListData> data, boolean isUpdate) {
						
					}
				},this);
		nsRequestService.deleteCache();
		nsRequestService.StartPost();
	}
@Override
protected void onStart() {
	// TODO Auto-generated method stub
	super.onStart();
	getData();
}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.menu_bar:
			showMenu();
			break;
		case R.id.search_bar:
			replaceFragmentWithAnimation(new NSStoreOnMapFragment());
			break;
		case R.id.home_menu:
			showContent();
			break;
		case R.id.signup:
			showContent();
			break;
		case R.id.post:
			startActivity(new Intent(this, NSPostFeedActivity.class));
			break;
		default:
			break;
		}
	}

	// replace Fragment
	protected void replaceFragmentNoBackStackWithAnimation(Fragment fragment) {
		final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		transaction.setCustomAnimations(R.anim.fragment_slide_right_enter, R.anim.fragment_slide_left_exit, R.anim.fragment_slide_left_enter,
				R.anim.fragment_slide_right_exit);
		transaction.replace(R.id.content, fragment);
		transaction.commit();
	}

	// replace Fragment whith animation
	protected void replaceFragmentWithAnimation(Fragment fragment) {
		final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		transaction.setCustomAnimations(R.anim.fragment_slide_right_enter, R.anim.fragment_slide_left_exit, R.anim.fragment_slide_left_enter,
				R.anim.fragment_slide_right_exit);
		transaction.replace(R.id.content, fragment);
		transaction.addToBackStack(fragment.getClass().getName());
		transaction.commit();
	}


	@Override
	public void ShowProcess() {
		// TODO Auto-generated method stub
		progressDialog.show();	
	}


	@Override
	public void HideProcess() {
		// TODO Auto-generated method stub
		progressDialog.dismiss();
	}
	@SuppressWarnings("unused")
	private void setMessageProcess(String TextMessage){
		progressDialog.setMessage(TextMessage);
	}
}
